package com.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.entity.Trend;
import com.entity.User;
import com.util.DBHelp;
import com.util.PageHelp;
import com.util.RowMapper;

public class TrendDao {
	private DBHelp<Trend> db=new DBHelp<Trend>();
	
	private class TrendRowMapper implements RowMapper<Trend>{

		public Trend mapRow(ResultSet rs) throws SQLException {
			Trend t=new Trend();
			t.setContent(rs.getString("content"));
			t.setCreateTime(rs.getString("createtime"));
			t.setUserid(rs.getString("userid"));
			t.setProjectid(rs.getString("projectid"));
			User u=new User();
			t.setId(rs.getString("id"));
			u.setId(rs.getString("userid"));
			u.setImage(rs.getString("image"));
			u.setUserName(rs.getString("username"));
			t.setUser(u);
			return t;
		}
	}

	public PageHelp<Trend> findByPage(PageHelp<Trend> page){
		String sql="select td.createtime,td.content,td.id,td.userid,td.projectid,tu.image,tu.username from t_trend AS td left join t_user AS tu on td.userid=tu.id order by td.createtime desc limit ?,?";
		List<Trend> list= db.executeQueryForList(sql, new TrendRowMapper(),page.getOffSet(),page.getPageSize());
		page.setResult(list);
		return page;
	}
	
	
	public Long getCount() {
			String sql="select count(id) from t_trend";
			return (Long) db.executeQueryForFunction(sql);
	}

	
	public void saveTrend(Trend t) {
		// TODO Auto-generated method stub
		String sql="insert into t_trend(id,content,createtime,userid,projectid) values (?,?,?,?,?)";
		db.executeSQL(sql, t.getId(),t.getContent(),t.getCreateTime(),t.getUserid(),t.getProjectid());
	}
	
}
